Bot Chat
接口说明
使用该接口,可以实现使用Http请求与星辰应用开发平台创建的Bot进行流式、非流式对话。可以用于集成业务系统中。使用前,请先在星辰应用开发平台创建可用的Bot并发布。
请求地址
https://xingchen-api.xf-yun.com/agent/v1/chat/completions
请求方法
Http POST
鉴权
该接口使用API key进行鉴权。
进入星辰大模型应用平台,点击个人中心,进入Bots菜单Tab界面,点击bot卡片中的信息,进入API秘钥界面。
获取APIKey
、APISecret
和BotID
。 鉴权 token
由 APIKey
和 APISecret
以冒号组合:
API_TOKEN = APIKey:APISecret
所有 API 请求都应在Authorization
HTTP 标头中包含您的 API_TOKEN
密钥信息。
格式:
Authorization: Bearer API_TOKEN
示例:
Authorization: Bearer 09754ce66fe9baaadcc5f49de7776cmc:MDU5YjM4ODRmZGMxMDdiMTg3YzFmZtQz
请求示例
curl
curl --location --request POST 'https://agent.xf-yun.com/agent/v1/chat/completions' \
--header 'Authorization: Bearer 09754ce66fe9baaadcc5f49de7776cmc:MDU5YjM4ODRmZGMxMDdiMTg3YzFmZtQz' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Content-Type: application/json' \
--data '{
"bot_id": "ef6123b9a427404b99b475f0134f2fcb",
"uid": "123",
"messages": [
{
"role": "user",
"content": "你好啊"
}
],
"stream": "true"
}'
openai sdk
from openai import OpenAI
client = OpenAI(
base_url="https://agent.xf-yun.com/v1",
api_key="09754ce66fe9baaadcc5f49de7776cmc:MDU5YjM4ODRmZGMxMDdiMTg3YzFmZtQz"
)
response = client.chat.completions.create(
model="", # 任意
messages=[{"role": "user", "content": "你好"}],
stream=True,
extra_body={
"bot_id": "ef6123b9a427404b99b475f0134f2fcb",
"uid": "123"
}
)
for resp in response:
print("response::", resp)
返回示例
流式
data: {"code":0,"message":"Success","id":"cha000c0076@dx191c21ce879b8f3532","created":123412324431,"choices":[{"delta":{"role":"assistant","content":"","tool_calls":[{"type":"tool","function":{"name":"天气查询","arguments":"{\"city\": \"北京\", \"date\": \"2024-09-13\"}", "response": ""}}]}, "finish_reason":null}],"usage":{"prompt_tokens":null,"completion_tokens":null,"total_tokens":null}}
data: {"code":0,"message":"Success","id":"cha000c0076@dx191c21ce879b8f3532","created":123412324431,"choices":[{"delta":{"role":"assistant","content":"","tool_calls":[{"type":"tool","function":{"name":"天气查询","arguments":"{\"city\": \"南京\", \"date\": \"2024-09-13\"}", "response": ""}}]}, "finish_reason":null}],"usage":{"prompt_tokens":null,"completion_tokens":null,"total_tokens":null}}
data: {"code":0,"message":"Success","id":"cha000c0076@dx191c21ce879b8f3532","created":123412324431,"choices":[{"delta":{"role":"assistant","content":"明天北京"}, "finish_reason":null}],"usage":{"prompt_tokens":null,"completion_tokens":null,"total_tokens":null}}
data: {"code":0,"message":"Success","id":"cha000c0076@dx191c21ce879b8f3532","created":123412324431,"choices":[{"delta":{"role":"assistant","content":"的天气"}, "finish_reason":null}],"usage":{"prompt_tokens":null,"completion_tokens":null,"total_tokens":null}}
data: {"code":0,"message":"Success","id":"cha000c0076@dx191c21ce879b8f3532","created":123412324431,"choices":[{"delta":{"role":"assistant","content":"晴转多云"}, "finish_reason":null}],"usage":{"prompt_tokens":null,"completion_tokens":null,"total_tokens":null}}
data: {"code":0,"message":"Success","id":"cha000c0076@dx191c21ce879b8f3532","created":123412324431,"choices":[{"delta":{"role":"assistant","content":"气温是3"}, "finish_reason":null}],"usage":{"prompt_tokens":null,"completion_tokens":null,"total_tokens":null}}
data: {"code":0,"message":"Success","id":"cha000c0076@dx191c21ce879b8f3532","created":123412324431,"choices":[{"delta":{"role":"assistant","content":"0.4"}, "finish_reason":null}],"usage":{"prompt_tokens":null,"completion_tokens":null,"total_tokens":null}}
data: {"code":0,"message":"Success","id":"cha000c0076@dx191c21ce879b8f3532","created":123412324431,"choices":[{"delta":{"role":"assistant","content":"℃, 南"}, "finish_reason":null}],"usage":{"prompt_tokens":null,"completion_tokens":null,"total_tokens":null}}
data: {"code":0,"message":"Success","id":"cha000c0076@dx191c21ce879b8f3532","created":123412324431,"choices":[{"delta":{"role":"assistant","content":"京的天"}, "finish_reason":null}],"usage":{"prompt_tokens":null,"completion_tokens":null,"total_tokens":null}}
data: {"code":0,"message":"Success","id":"cha000c0076@dx191c21ce879b8f3532","created":123412324431,"choices":[{"delta":{"role":"assistant","content":"气晴,气"}, "finish_reason":null}],"usage":{"prompt_tokens":null,"completion_tokens":null,"total_tokens":null}}
data: {"code":0,"message":"Success","id":"cha000c0076@dx191c21ce879b8f3532","created":123412324431,"choices":[{"delta":{"role":"assistant","content":"温35℃"}, "finish_reason":null}],"usage":{"prompt_tokens":32,"completion_tokens":28,"total_tokens":60}}
data: [DONE]
非流式
{
"code":0,
"message":"Success",
"id":"cha000c0076@dx191c21ce879b8f3532",
"created": 123412324431,
"choices":[
{
"message":{
"role":"assistant",
"content":"北京明天阴转多云,气温xx, 南京明天晴天,气温xxx...",
"tool_calls": [
{
"type": "tool",
"function": {
"name": "天气查询",
"arguments": "{\"city\": \"北京\", \"date\": \"2024-09-13\"}",
"response": ""
}
},
{
"type": "tool",
"function": {
"name": "天气查询",
"arguments": "{\"city\": \"南京\", \"date\": \"2024-09-13\"}",
"response": ""
}
}
]
},
"finish_reason": "stop"
}
],
"usage":{
"prompt_tokens":25,
"completion_tokens":26,
"total_tokens":51
}
}
请求参数说明
参数 | 取值 | 是否必传 | 说明 |
---|---|---|---|
bot_id | String | 是 | bot id,会校验与 app id 匹配的正确性。 |
uid | String | 否 | uid,业务方维护。 |
messages | Array[Object] | 是 | 请求消息,格式如下: [ {"role": "user", "content": "q1"}, {"role": "assistant", "content": "a1"}, {"role": "user", "content": "q2"} ] |
stream | bool | 是 | 是否启用流式返回。流式:true非流式:false |
返回参数示例
流式
参数名称 | 类型 | 参数说明 |
---|---|---|
code | Int | 错误码,0表示正常,非0表示出错;详细释义可在接口说明文档最后的错误码说明了解。 |
message | String | 会话是否成功的描述信息。 |
id | String | 会话的唯一id,同时可用于技术人员查询服务端会话日志使用,出现调用错误时建议留存该字段。 |
created | Number | 对话创建时间。 |
choices | Array[Obect] | |
choices[0].finish_reason | String | 停止。 |
choices[0].delta | Object | 当前帧。 |
choices[0].delta.role | String | 角色。 |
choices[0].delta.content | String | 内容,角色回复的内容。 |
choices[0].delta.tool_calls | Array[Object] | |
choices[0].delta.tool_calls[0].type | String | tool类型,为tool或者workflow。 |
choices[0].delta.tool_calls[0].function | Object | |
choices[0].delta.tool_calls[0].function.name | String | 插件名称。 |
choices[0].delta.tool_calls[0].function.arguments | String | 插件参数。 |
choices[0].delta.tool_calls[0].function.response | String | 插件输出。 |
usage | Object | token计量。 |
usage.prompt_tokens | int | 请求token。 |
usage.completion_tokens | int | 回复token。 |
usage.total_tokens | int | 总token。 |
非流式
参数名称 | 类型 | 参数说明 |
---|---|---|
code | Int | 错误码,0表示正常,非0表示出错;详细释义可在接口说明文档最后的错误码说明了解。 |
message | String | 会话是否成功的描述信息。 |
id | String | 会话的唯一id,同时可用于技术人员查询服务端会话日志使用,出现调用错误时建议留存该字段。 |
created | Number | 对话创建时间。 |
choices | Array[Obect] | |
choices[0].finish_reason | String | 停止。 |
choices[0].message | Object | 当前帧。 |
choices[0].message.role | String | 角色。 |
choices[0].message.content | String | 内容。 |
choices[0].message.tool_calls | Array[Object] | |
choices[0].message.tool_calls[0].type | String | tool类型,为tool或者workflow。 |
choices[0].message.tool_calls[0].function | Object | |
choices[0].message.tool_calls[0].function.name | String | 插件名称。 |
choices[0].message.tool_calls[0].function.arguments | String | 插件参数。 |
choices[0].message.tool_calls[0].function.response | String | 插件输出。 |
usage | Object | token计量。 |
usage.prompt_tokens | int | 请求token。 |
usage.completion_tokens | int | 回复token。 |
usage.total_tokens | int | 总token。 |
错误码
错误码 | 说明 |
---|---|
40000 | 服务错误 |
40001 | Bot配置找不到 |
40002 | 请求体校验失败 |
40003 | 无效的Bot配置 |
40004 | 知识库检索失败 |
40006 | app id错误 |
40007 | 执行插件失败 |
40009 | 请求大模型失败 |
40010 | 解析推理过程失败 |
40011 | 请求大模型出错 |
40021 | 解析模型输出出错 |